const cloudHelper = require('../../../../../helper/cloud_helper.js');
const pageHelper = require('../../../../../helper/page_helper.js');

Page({
	data: {
		isLoad: false,
		detail: null
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		if (!options.id) {
			pageHelper.showNoneToast('记录不存在');
			return;
		}

		this._loadDetail(options.id);
	},

	/**
	 * 加载详情
	 */
	async _loadDetail(id) {
		try {
			const result = await cloudHelper.callCloudData('qa/history/detail', {
				id
			});

			this.setData({
				isLoad: true,
				detail: result
			});
		} catch (err) {
			console.error(err);
			this.setData({
				isLoad: true
			});
			pageHelper.showModal('加载失败，请重试');
		}
	},

	/**
	 * 再次提问
	 */
	bindReuse: function () {
		const question = this.data.detail.question;
		if (!question) return;

		wx.navigateTo({
			url: '../index/qa_index?content=' + encodeURIComponent(question)
		});
	},

	/**
	 * 返回问答主页
	 */
	bindBack: function() {
		wx.switchTab({
			url: '../index/qa_index',
		});
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {
		return {
			title: '电力智能问答助手',
			path: '/projects/repair/pages/qa/index/qa_index'
		}
	},

	async bindDelete() {
		const id = this.data.detail._id;
		if (!id) return;

		try {
			await pageHelper.showConfirm('确定删除此记录？');
			
			await cloudHelper.callCloudData('qa/history/delete', {
				id
			});

			pageHelper.showSuccToast('删除成功');
			
			setTimeout(() => {
				wx.navigateBack();
			}, 1500);
		} catch (err) {
			console.error(err);
		}
	}
}); 